package com.example.junior.activities;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import basicas.AoClicarNoTimeListener;
import basicas.AoMudarFavoritoListener;
import basicas.Time;
import fragments.DetalheTimeFragment;
import fragments.ListaFavoritoFragment;
import fragments.ListaTimeFragment;


public class TimeActivity extends ActionBarActivity  implements AoClicarNoTimeListener, AoMudarFavoritoListener{

    ViewPager mViewPager;
    ListaTimeFragment mlistaTimeFragment;
    ListaFavoritoFragment mfavoritoFragment;

    public static final int REQUEST_CODE_DETALHE = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time);

        FragmentManager fm = getSupportFragmentManager();
        mViewPager = (ViewPager)findViewById(R.id.viewPager);

        mlistaTimeFragment = (ListaTimeFragment)fm.findFragmentByTag("android:switcher:" + mViewPager.getId() + ":" + 0);
        mfavoritoFragment = (ListaFavoritoFragment)fm.findFragmentByTag("android:switcher:" + mViewPager.getId() + ":" + 1);

        if(mlistaTimeFragment == null){
            mlistaTimeFragment = new ListaTimeFragment();
        }

        if(mfavoritoFragment == null){
            mfavoritoFragment = new ListaFavoritoFragment();
        }
        TimesPagerAdapter adapter = new TimesPagerAdapter(fm);

        mViewPager.setAdapter(adapter);

        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        // Sincronizar abas com viewPager
        ActionBar.TabListener listener = new ActionBar.TabListener() {
            @Override
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
                mViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
            }

            @Override
            public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
            }
        };
        // Sincronizar viewPager com as abas
        ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {
            }

            @Override
            public void onPageSelected(int i) {
                actionBar.setSelectedNavigationItem(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {
            }
        };
        mViewPager.setOnPageChangeListener(pageChangeListener);


        for (int i = 0; i < adapter.getCount(); i++){
            ActionBar.Tab aba = actionBar.newTab();
            aba.setText(adapter.getPageTitle(i));
            aba.setTabListener(listener);
            actionBar.addTab(aba);
        }
    }

    @Override
    public void aoClicarNoTime(Time time, boolean usuarioClicou) {
        // É tablet
        if (getResources().getBoolean(R.bool.tablet)){
            DetalheTimeFragment detalhetimeFragment =
                    DetalheTimeFragment.newInstance(time);

            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.container, detalhetimeFragment, "detalhe")
                    .commit();

            // É smartphone
        } else {
            if (usuarioClicou) {
                Intent it = new Intent(this, DetalheTimeActivity.class);
                it.putExtra(DetalheTimeActivity.EXTRA_TIME,time);
                startActivityForResult(it, REQUEST_CODE_DETALHE);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_DETALHE &&
                resultCode == RESULT_OK){
            mfavoritoFragment.atualizarTela();
        }
    }

    @Override
    public void aoMudarFavorito(Time time, boolean favorito) {
        mfavoritoFragment.atualizarTela();
    }


    class TimesPagerAdapter extends FragmentPagerAdapter{

        TimesPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            if(i==1){
                return mfavoritoFragment;
            }
            return mlistaTimeFragment;
        }
        @Override
        public CharSequence getPageTitle(int position) {
            if (position == 0) {
                return "Times";
            }
            else return "Favoritos";
        }
        @Override
        public int getCount() {
            return 2;
        }
    }
}
